package com.chenl.provider.util;

import cn.hutool.core.collection.CollUtil;
import com.chenl.provider.DTO.SysUser;
import com.chenl.provider.util.checker.Checker;
import com.chenl.provider.util.checker.Checkers;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.function.Supplier;

public class CheckUtils {
    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            long start = System.currentTimeMillis();
            SysUser sysUser = new SysUser();
            sysUser.setId(0L);
            sysUser.setName("");
            sysUser.setAge(10);
            sysUser.setCreateBy("");
            sysUser.setCreateTime(new Date());
            sysUser.setUpdateBy("");
            sysUser.setUpdateTime(new Date());
            sysUser.setDeleteFlag(0);
            sysUser.setVersion(0);

            Checker<SysUser> checker = Checkers.<SysUser>lambdaCheck()
                    .notNull(SysUser::getName)
                    .ne(SysUser::getAge, 0)
                    .custom(item -> item.getAge() > queryByDb(item.getId()), "年龄异常");
            checker.check(sysUser);
            System.out.println("cost " + (System.currentTimeMillis() - start));
        }

    }

    /**
     * 模拟通过db获取判断值进行自定义判断
     */
    private static Integer queryByDb(Long id) {
        if (id > 0) {
            return -1;
        } else {
            return 1;
        }
    }

//    public boolean check(){
//        User user = new User();
//        user.setName("zs");
//        boolean b = CheckUtils.checkPropertysIsBlank(User::getName);
//    }

    public static boolean checkPropertysIsBlank(Supplier<?>... suppliers) {
        List<Supplier> supplierList = Arrays.asList(suppliers);
        for (Supplier supplier : supplierList) {
            Object o = supplier.get();
            if (o instanceof List) {
                if (CollUtil.isEmpty((List<?>) o)) {
                    return false;
                }
            } else if (o instanceof String) {
                if (CollUtil.isEmpty((List<?>) o)) {
                    return false;
                }
            }
        }
        return true;
    }
}
